package com.mocai.merchant.core.domain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页信息
 */
public class Page implements Pageable {
    // 页码
    private int pageNumber;
    // 页面大小
    private int pageSize;
    // 总记录数
    private long totalElements;
    // 页面数据
    private List<?> content = new ArrayList<>();

    public Page() {
    }

    public Page(int pageNumber, int pageSize) {
        if (pageNumber < 0) {
            throw new IllegalArgumentException("Page index must not be less than zero!");
        }

        if (pageSize < 1) {
            throw new IllegalArgumentException("Page size must not be less than one!");
        }
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
    }

    @Override
    public int getPageNumber() {
        return pageNumber;
    }

    @Override
    public int getPageSize() {
        return pageSize;
    }

    @Override
    public int getOffset() {
        return pageNumber * pageSize;
    }

    @Override
    public Pageable next() {
        return new Page(getPageNumber() + 1, getPageSize());
    }

    @Override
    public Pageable previous() {
        return new Page(getPageNumber() - 1, getPageSize());
    }

    @Override
    public Pageable previousOrFirst() {
        return hasPrevious() ? previous() : first();
    }

    @Override
    public Pageable first() {
        return new Page(0, getPageSize());
    }

    @Override
    public boolean hasPrevious() {
        return pageNumber > 0;
    }

    @Override
    public boolean hasNext() {
        return getPageNumber() + 1 < getTotalPages();
    }

    @Override
    public boolean isFirst() {
        return !hasPrevious();
    }

    @Override
    public boolean isLast() {
        return !hasNext();
    }

    @Override
    public long getTotalElements() {
        return totalElements;
    }

    @Override
    public int getTotalPages() {
        return (int) Math.ceil((double) getTotalElements() / (double) getPageSize());
    }

    @Override
    public boolean hasContent() {
        return !content.isEmpty();
    }

    @Override
    public List<?> getContent() {
        return content;
    }

    @Override
    public int getSizeOfContent() {
        return content.size();
    }

    public void setTotalElements(long totalElements) {
        this.totalElements = totalElements;
    }

    public void setContent(List<?> content) {
        this.content = content;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    // 转换为前端插件 DataTable 所需的数据格式
    // "draw": 1, "recordsTotal": 57, "recordsFiltered": 57, "data":[]
    public Object asDataTable() {
        Map retVal = new HashMap();
        retVal.put("draw", getPageNumber());
        retVal.put("recordsTotal", getPageSize());
        retVal.put("recordsFiltered", getTotalElements());
        retVal.put("data", getContent());
        return retVal;
    }

}
